package log

import (
	"errors"
	"strings"

	"github.com/kolonse/logs"
)

// Log levels to control the logging output.
const (
	LevelEmergency = iota
	LevelAlert
	LevelCritical
	LevelError
	LevelWarning
	LevelNotice
	LevelInformational
	LevelDebug
)

// SetLogLevel sets the global log level used by the simple
// logger.
func SetLevel(l int) {
	beeLogger.SetLevel(l)
}

func SetLogFuncCall(b bool) {
	beeLogger.EnableFuncCallDepth(b)
	beeLogger.SetLogFuncCallDepth(3)
}

func defaultLogs() *logs.BeeLogger {
	ret := logs.NewLogger(10000)
	err := ret.SetLogger("console", "")
	if err != nil {
		panic(err)
	}
	ret.EnableFuncCallDepth(true)
	ret.SetLogFuncCallDepth(3)
	return ret
}

// logger references the used application logger.
var beeLogger *logs.BeeLogger

// SetLogger sets a new logger.
func SetLogger(adaptername string, config string) error {
	err := beeLogger.SetLogger(adaptername, config)
	if err != nil {
		return err
	}
	return nil
}

func Println(v ...interface{}) {
	beeLogger.Debug(generateFmtStr(len(v)), v...)
}

func Panicln(v ...interface{}) {
	beeLogger.Emergency(generateFmtStr(len(v)), v...)
	panic(errors.New("Panic Log"))
}

func generateFmtStr(n int) string {
	return strings.Repeat("%v ", n)
}

func Logger() *logs.BeeLogger {
	return beeLogger
}

func init() {
	beeLogger = defaultLogs()
}
